import { Success } from '../../code/http-exception';
import auth from '../../middlewares/auth';
import User from '../../sql/module/user/user';
import Menu from '../../sql/module/menu';
import Role from '../../sql/module/user/role';
import RoleMenu from '../../sql/module/_dictionaries/role_menu';
import UserRole from '../../sql/module/_dictionaries/user_role';
import { sequelize } from '../../sql/db';

const visitor: number[] = []
const main: number[] = []
const user: number[] = []
const admin: number[] = [2, 3, 18, 20, 19, 21, 22, 23, 25, 24, 5, 6, 7, 8]
const router: Api = new Map()

const resetMenu = async () => {
    const menus = [
        { id: 1, label: '主页', url: '_dashboard', type: 1, img: 'wap-home-o' },
        { id: 2, label: '账号', url: '_user', type: 1, img: 'wap-home-o' },
        { id: 3, label: '账号管理', url: 'user', type: 1, upLevel: '_user' },
        { id: 4, label: '权限管理', url: 'power', type: 1, upLevel: '_user' },
        { id: 5, label: '区域管理', url: 'area', type: 1, upLevel: '_user' },
        {
            id: 6,
            label: '区域列表', url: 'areaList', type: 2, upLevel: 'area',
            img: '/icon/icon.png'
        },
        { id: 7, label: '添加区域', url: 'addArea', type: 3, upLevel: 'areaList' },
        { id: 8, label: '删除', url: 'deleteArea', type: 3, upLevel: 'areaList' },
        {
            id: 9,
            label: '菜单列表', url: 'menu', type: 2, upLevel: 'power',
            img: '/icon/icon.png'
        },
        { id: 10, label: '编辑', url: 'editMenu', type: 3, upLevel: 'menu' },
        { id: 11, label: '启用/禁用', url: 'menuState', type: 3, upLevel: 'menu' },
        { id: 12, label: '重置菜单', url: 'resetMenu', type: 3, upLevel: 'menu' },
        {
            id: 13,
            label: '身份列表', url: 'rolelist', type: 2, upLevel: 'power',
            img: '/icon/icon.png'
        },
        { id: 14, label: '添加身份', url: 'addRole', type: 3, upLevel: 'rolelist' },
        { id: 15, label: '重置身份', url: 'resetRole', type: 3, upLevel: 'rolelist' },
        { id: 16, label: '编辑', url: 'updateRole', type: 3, upLevel: 'rolelist' },
        { id: 17, label: '删除', url: 'deleteRole', type: 3, upLevel: 'rolelist' },
        {
            id: 18,
            label: '账号列表', url: 'userList', type: 2, upLevel: 'user',
            img: '/icon/icon.png'
        },
        { id: 19, label: '添加账号', url: 'addUser1', type: 3, upLevel: 'userList' },
        {
            id: 20,
            label: '添加账号', url: 'addUser2', type: 2, upLevel: 'user',
            img: '/icon/icon.png'
        },
        { id: 21, label: '详情', url: 'userDetail2', type: 3, upLevel: 'userList' },
        { id: 22, label: '重置密码', url: 'resetPassword', type: 3, upLevel: 'userList' },
        { id: 23, label: '编辑', url: 'userDetail1', type: 3, upLevel: 'userList' },
        { id: 24, label: '编辑', url: 'updataUser', type: 3, upLevel: 'userDetail2' },
        { id: 25, label: '删除', url: 'deleteUser', type: 3, upLevel: 'userList' },
        {
            id: 26, label: '反馈列表', url: 'suggestList', type: 2, upLevel: '_user',
            img: '/icon/icon.png'
        },
        { id: 27, label: '删除', url: 'deleteRecord', type: 3, upLevel: 'recordList' },
        { id: 28, label: '记录详情', url: 'recordDetail', type: 3, upLevel: 'recordList' },
        { id: 29, label: '个人页', url: '_account', type: 1, public: 1, img: "user-o" },
        { id: 30, label: '个人中心', url: 'account', type: 2, upLevel: '_account', public: 1 },
        { id: 31, label: '个人设置', url: 'basicset', type: 2, upLevel: '_account', public: 1 },
        { id: 32, label: '编辑', url: 'changeInfo', type: 3, upLevel: 'account', public: 1 },
        { id: 33, label: '修改密码', url: 'changePassword', type: 3, upLevel: 'account', public: 1 },
        { id: 34, label: '未读消息', url: 'message', type: 2, upLevel: '_account', public: 1, isPhone: 1 },
        { id: 35, label: '问题反馈与意见', url: 'suggest', type: 2, upLevel: '_account', public: 1, delete: true },
        {
            id: 36,
            label: '记录列表', url: 'recordList', type: 2, upLevel: '_user',
            img: '/icon/icon.png'
        },
        { id: 37, label: '我的反馈', url: 'suggestSelfList', type: 3, upLevel: 'suggest', public: 1 },
        { id: 38, label: '详情', url: 'suggestSelfInfo', type: 3, upLevel: 'suggestSelfList', public: 1 },
        { id: 39, label: '详情', url: 'suggestInfo', type: 3, upLevel: 'suggestList' },
        { id: 40, label: '回复', url: 'addSuggest', type: 3, upLevel: 'suggestInfo' },
        { id: 41, label: '删除', url: 'suggestDelete', type: 3, upLevel: 'suggestSelfList', public: 1 },
        { id: 42, label: '恢复', url: 'recover', type: 3, upLevel: 'recordDetail' },
        { id: 43, label: '会话', url: 'C2CList', type: 3, upLevel: 'basicset', public: 1 },
        { id: 44, label: 'Dashboard', url: 'dashboard', type: 2, upLevel: '_dashboard' },
        { id: 999, label: '问题反馈与意见', url: 'suggest', type: 2, upLevel: '_account', public: 1 },
    ]
    await Promise.all(menus.map(async item => {
        if (item.delete) await Menu.destroy({ where: { id: item.id } })
        else {
            const res = await Menu.update({ isPhone: 0, icon: '', img: '', public: null, ...item }, { where: { id: item.id } })
            if (!res[0]) await Menu.create(item)
        }
    }))
}
const resetRole = async () => {
    await Role.create({
        id: 1,
        uid: 100,
        name: 'superAdmin'
    })
    await Role.bulkCreate([{
        id: 2,
        uid: 1,
        name: 'visitor',
    }, {
        id: 3,
        uid: 2,
        name: 'user',
    }, {
        id: 4,
        uid: 3,
        name: 'main',
    }, {
        id: 5,
        uid: 99,
        name: 'admin',
    }])
    await RoleMenu.bulkCreate(visitor.map(item => ({
        MenuId: item,
        RoleId: 2
    })))
    await RoleMenu.bulkCreate(user.map(item => ({
        MenuId: item,
        RoleId: 3
    })))
    await RoleMenu.bulkCreate(main.map(item => ({
        MenuId: item,
        RoleId: 4
    })))
    await RoleMenu.bulkCreate(admin.map(item => ({
        MenuId: item,
        RoleId: 5
    })))
}
const setAdmin = async () => {
    const hasAdmin = await UserRole.findOne({
        where: {
            RoleId: 1
        }
    })
    if (!hasAdmin) {
        const [user] = await User.findOrCreate({
            where: {
                phone: 'admin'
            }, defaults: {
                phone: 'admin',
                password1: 'admin',
                nickname: '系统管理员'
            }
        })
        await UserRole.create({
            UserId: user.id,
            RoleId: 1
        })
    }
}
//type 菜单1，路由地址2，按钮3
router.set('/resetMenu', [auth('resetMenu'), async () => {
    await resetMenu()
    throw new Success()
}])

router.set(['/resetRole', 'get'], [async () => {
    await resetRole()
    throw new Success()
}])

router.set('/admin', async () => {
    await setAdmin()
    throw new Success()
})
//init时打开
router.set(['/init', 'get'], async () => {
    await sequelize.sync()
    await resetMenu()
    await resetRole()
    await setAdmin()
    throw new Success()
})

router.set(['/sync', 'get'], async () => {
    await sequelize.sync()
    throw new Success()
})

export { router }